package com.lemon.bean.stageBean;

import com.lemon.bean.MyMenuContext;
import com.lemon.bean.MyTreeItem;
import com.lemon.util.StringUtil;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.effect.BoxBlur;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Effect;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.Mnemonic;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.transform.Transform;
import javafx.stage.Stage;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import static com.lemon.beanManager.BeanContainer.beanFactory;

public class LoadMainView {
    private Stage stage = new Stage();
    //todo:加功能
    private Button allSelect;
    private TextField dirLocationStr;
    private AnchorPane imageViewOuter;
    private Button copyButton;
    private Button pasteButton;
    private ScrollPane scroll;
    private Label selectInfo;
    private TextField searchTextField;
    private Button close;
    private Button max;
    private Button min;
    private Button search;
    private TreeView<String> treeView;
    private FlowPane imgViewBox;

    private Pane mainViewWindow;

    public void initFunction() {
        //初始化目录树
        MyTreeItem root = (MyTreeItem) beanFactory.getBean("treeView");
        this.treeView.setShowRoot(false);
        this.treeView.setRoot(root);
        imgViewBox.setHgap(10);
        imgViewBox.setVgap(10);
        imgViewBox.setPadding(new Insets(15, 0, 15, 15));

        //注册右上角
        min.setOnMouseClicked(event -> {
            min.setFocusTraversable(false);
            minOnClick();
        });
        max.setOnMouseClicked(event -> {
            max.setFocusTraversable(false);
            maxOnClick();
        });
        close.setOnMouseClicked(event -> {
            close.setFocusTraversable(false);
            closeOnClick();
        });
        //开启矩形选中框
        this.selectedRectangle(rb);
        //注册搜索功能
        search.setOnMouseClicked(e -> {
            searchOnClick();
        });
        //注册复制
        myMenuContext.setCopyFunction(e -> {
            copyClick();
        });
        copyButton.setOnAction(e -> {
            copyClick();
            copyButton.setFocusTraversable(false);
        });
        //注册粘贴
        myMenuContext.setPasteFunction(e -> {
            pasteClick();
        });
        pasteButton.setOnAction(e -> {
            pasteClick();
            pasteButton.setFocusTraversable(false);
        });
        //
        searchTextField.setOnKeyPressed(e -> {
            if (!searchTextField.getText().equals("")) {
                search.setStyle("-fx-border-color: #70c0e7;\n" +
                        "-fx-border-width: 1;\n" +
                        "-fx-background-color: #e5f3fb;");
                search.setOpacity(1);
            } else {
                search.setStyle("");
                search.setOpacity(0);
//                searchTextField.setPrefWidth(searchTextField.getWidth());
            }
        });
        //设置重命名按钮
        myMenuContext.setRenameFunction(e -> {
            long selectNum = 0;
            Set<Node> nodes = imgViewBox.lookupAll("ImageView");
            System.out.println(nodes.size());
            List<Text> textList = new ArrayList<>();
            List<String> filenameList = new ArrayList<>();
            for (Node node : nodes) {
                if (node.getParent().getParent().getParent().getOpacity() != 1) {
                    selectNum++;
                    Text text = (Text) node.getParent().getParent().getParent().lookup("Text");
                    textList.add(text);
                    String filename = ((ImageView) node).getImage().getUrl().replaceAll("file:", "");
                    filenameList.add(filename);
                }
            }
            if (selectNum == 0) {
                return;
            } else if (selectNum == 1) {
                LoadSingleRename lsr = new LoadSingleRename();
                lsr.loadView();
                lsr.initialFunction();
                lsr.getTitle().setText("单个图片改名");
                lsr.show();
                lsr.setRenameImage(textList.get(0), filenameList.get(0));
            } else {
                LoadMutiRename lmr = new LoadMutiRename();
                lmr.loadView();
                lmr.initialFunction();
                lmr.show();
                lmr.setWaitRenameTextList(textList);
                lmr.setWaitRenameFilenameList(filenameList);
            }
        });

        //设置删除按钮
        myMenuContext.setDeleteFunction(e -> {
            int selectNum = 0;
            Set<Node> nodes = imgViewBox.lookupAll("ImageView");
            for(Node node:nodes){
                if (node.getParent().getParent().getParent().getOpacity() == 0.95) {
                    selectNum++;
                }
            }
            LoadDelete ld = new LoadDelete();
            ld.loadDelete();
            ld.initFunction();
            ld.setTitle("确认删除"+selectNum+"张照片");
            ld.show();
            ld.setFlowPane(imgViewBox);

            /*
            for (Node node : nodes) {
                if (node.getParent().getParent().getParent().getOpacity() == 0.95) {
                    ImageView iv = (ImageView) node;
                    imgViewBox.getChildren().remove(node.getParent().getParent().getParent());
                    selectNum++;
                    String str = iv.getImage().getUrl().replaceAll("file:", "");
                    iv.setImage(null);
                    System.gc();
                    FileUtils.fileDelete(str);
                }
            }*/
        });

        //注册点击面板
        imgViewBox.setOnMouseClicked(this::imgBoxOnClick);
        //注册treeView点击
        treeView.setOnMouseClicked(event -> {
            treeViewOnClick();
        });
        imgViewBox.addEventFilter(MouseEvent.MOUSE_ENTERED_TARGET, e -> {
            enterImageView(e);
        });
        imgViewBox.addEventFilter(MouseEvent.MOUSE_EXITED_TARGET, e -> {
            exitImageView(e);
        });
        allSelect.setOnAction(e -> {
            selectAllImage();
        });
        //添加css
        URL resource = this.getClass().getResource("css/mainView.css");
        addStyle(resource.toExternalForm());
    }

    /**
     * @Date 2022/05/10
     */
    private void selectAllImage() {
        imgViewBox.getChildren().forEach(child -> {
            selectStyle(child);
        });
    }

    private void exitImageView(MouseEvent mouseEvent) {
        if (mouseEvent.getTarget() instanceof AnchorPane) {
            AnchorPane imageView = (AnchorPane) mouseEvent.getTarget();
            //点击事件
            ImageView iv = (ImageView) imageView.lookup("ImageView");
            VBox vBox = (VBox) iv.getParent().getParent().getParent();
            cancelEnterStyle(vBox);
        } else if (mouseEvent.getTarget() instanceof ImageView) {//点击图片位置
            ImageView iv = (ImageView) mouseEvent.getTarget();
            VBox vBox = (VBox) iv.getParent().getParent().getParent();
            cancelEnterStyle(vBox);
        } else if (mouseEvent.getTarget() instanceof VBox) {
            VBox vBox = (VBox) mouseEvent.getTarget();
            if (vBox.getChildren().size() == 1) {
                cancelEnterStyle(vBox.getParent().getParent());
                return;
            }
            cancelEnterStyle(vBox);
        } else if (mouseEvent.getTarget() instanceof Text) {
            Text text = (Text) mouseEvent.getTarget();
            VBox vBox = (VBox) text.getParent();
            cancelEnterStyle(vBox);
        }
    }


    private void enterImageView(MouseEvent mouseEvent) {
        if (mouseEvent.getTarget() instanceof AnchorPane) {
            AnchorPane imageView = (AnchorPane) mouseEvent.getTarget();
            //点击事件
            ImageView iv = (ImageView) imageView.lookup("ImageView");
            VBox vBox = (VBox) iv.getParent().getParent().getParent();
            enterStyle(vBox);
        } else if (mouseEvent.getTarget() instanceof ImageView) {//点击图片位置
            ImageView iv = (ImageView) mouseEvent.getTarget();
            VBox vBox = (VBox) iv.getParent().getParent().getParent();
            enterStyle(vBox);
        } else if (mouseEvent.getTarget() instanceof VBox) {
            VBox vBox = (VBox) mouseEvent.getTarget();
            if (vBox.getChildren().size() == 1) {
                enterStyle(vBox.getParent().getParent());
                return;
            }
            enterStyle(vBox);
        } else if (mouseEvent.getTarget() instanceof Text) {
            Text text = (Text) mouseEvent.getTarget();
            VBox vBox = (VBox) text.getParent();
            enterStyle(vBox);
        }
    }


    public void loadMainView() {

        try {
            mainViewWindow = FXMLLoader.load(getClass().getResource("View.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(mainViewWindow, 1000, 500);
        stage.setScene(scene);
        stage.setWidth(1000);
        stage.setHeight(500);
        allSelect = (Button) beanFactory.getBean("mainAllSelect");
        dirLocationStr = (TextField) beanFactory.getBean("mainDirLocationStr");
        imageViewOuter = (AnchorPane) beanFactory.getBean("imageViewOuter");
        copyButton = (Button) beanFactory.getBean("mainCopyButton");
        pasteButton = (Button) beanFactory.getBean("mainPasteButton");
        scroll = (ScrollPane) beanFactory.getBean("mainScroll");
        selectInfo = (Label) beanFactory.getBean("mainSelectInfo");
        searchTextField = (TextField) beanFactory.getBean("mainSearchTextField");
        close = (Button) beanFactory.getBean("mainClose");
        max = (Button) beanFactory.getBean("mainMax");
        min = (Button) beanFactory.getBean("mainMin");
        search = (Button) beanFactory.getBean("mainSearch");
        treeView = (TreeView<String>) beanFactory.getBean("mainTreeView");
        imgViewBox = (FlowPane) beanFactory.getBean("mainImgViewBox");
        initMnemonic();
    }

    public void show() {
        stage.show();
    }

    public void shutdownStage() {
        stage.close();
    }

    //矩阵选中框(内部类)
    private RectBox rb = new RectBox();
    private MyMenuContext myMenuContext = MyMenuContext.getInstant();

    class RectBox {
        private HBox hBoxView;
        //矩形框的属性
        private double startX = 0.0;
        private double startY = 0.0;
        private double endX = 0.0;
        private double endY = 0.0;
        private boolean openDrag = true;
        private double computeXStart = 0.0;
        private double computeYStart = 0.0;
        private double computeXEnd = 0.0;
        private double computeYEnd = 0.0;
    }

    private void minOnClick() {
        stage.setIconified(true);
    }

    private void maxOnClick() {

        if (!stage.isMaximized()) {
            stage.setMaximized(true);
        } else {
            stage.setMaximized(false);
        }
    }

    private void closeOnClick() {
        shutdownStage();
    }

    private List<File> copyList = new LinkedList<>();

    private void copyClick() {
        copyList.clear();
        Set<Node> imageView = imgViewBox.lookupAll("ImageView");
        for (Node node : imageView) {
            if (node instanceof ImageView) {
                ImageView iv = (ImageView) node;
                double opacity = iv.getParent().getParent().getParent().getOpacity();
                if (opacity == 0.95) {//选中状态
                    String path = iv.getImage().getUrl().replace("file:", "");
                    copyList.add(new File(path));
                }
            }
        }
        System.out.println(copyList);
    }

    private void pasteClick() {
        MyTreeItem root = (MyTreeItem) treeView.getSelectionModel().getSelectedItem();
        if (root == null || root.getFile() == null) {
            return;
        }
        File destPath = root.getFile();
        for (File src : copyList) {
            File dest = new File(destPath, src.getName());

            if (dest.exists()) {
                int index = 1;
                String newName = src.getName();
                String prefix = newName.substring(0,newName.lastIndexOf("."));
                String fileType = newName.substring(newName.lastIndexOf("."));

                while ((dest = new File(destPath, newName)).exists()) {
                    newName = prefix + "副本" + (index++)+fileType;
                }
            }
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(src);
                fos = new FileOutputStream(dest);
                byte[] buffer = new byte[4096];
                int len;
                while ((len = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("复制完毕");
            List<VBox> list = collectionImage(destPath);
            for (VBox vBox : list) {
                imgViewBox.getChildren().add(vBox);
            }
        }
    }

    private void searchOnClick() {
        treeViewOnClick();
        String keyWord = searchTextField.getText();
        ObservableList<Node> children = imgViewBox.getChildren();
        List<Node> newList = new ArrayList<>();
        children.forEach(node -> {
            Text text = (Text) node.lookup("Text");
            if (text.getText().contains(keyWord)) {
                newList.add(node);
            }
        });
        imgViewBox.getChildren().removeAll(imgViewBox.getChildren());
        imgViewBox.getChildren().addAll(newList);
        changeImageInfo();
    }

    private void treeViewOnClick() {
        //获取点击的节点
        imgViewBox.getChildren().clear();
        System.gc();
        MyTreeItem root = (MyTreeItem) treeView.getSelectionModel().getSelectedItem();
        if (root != null) {
            File selectDir = root.getFile();
            if (selectDir == null) {//盘符未设置
                return;
            }
            List<VBox> boxList = collectionImage(selectDir);
            if (boxList == null) {
                return;
            }
            for (VBox box : boxList) {
                imgViewBox.getChildren().add(box);
            }
            dirLocationStr.setText(selectDir.getAbsolutePath());
        }
        if (root != null)
            changeImageInfo();
    }

    private List<VBox> collectionImage(File selectDir) {
        imgViewBox.getChildren().clear();
        List<VBox> list = new LinkedList<>();

        String[] filesStr = selectDir.list();
        if (filesStr == null) {
            return null;
        }
        File[] files = selectDir.listFiles();
        for (int i = 0; i < filesStr.length; i++) {
            if (StringUtil.isImg(filesStr[i])) {
//                stringList.add("file:" + files[i].getAbsolutePath());
                //VBox内含AnchorPane和Text，AnchorPane用于底部对齐
                VBox vBox = new VBox();
                vBox.setPadding(new Insets(10));
                vBox.setSpacing(10);
                //设置AnchorPane大小，并使子元素底部对齐
                AnchorPane picAnchor = new AnchorPane();
                picAnchor.setPrefWidth(100);
                picAnchor.setPrefHeight(100);
                //用VBox存ImageView，使得其缩放后可以居中对齐
                VBox interBox = new VBox();
//                interBox.setStyle("-fx-border-color: black");
                //此处宽度应该和AnchorPane一致
                interBox.setPrefWidth(100);
                //添加为AnchorPane子元素
                picAnchor.getChildren().add(interBox);
                //底部对齐
                AnchorPane.setBottomAnchor(interBox, 0.0);

                //文本数据
                Text text = new Text(files[i].getName());
                text.setWrappingWidth(100);
                text.setTextAlignment(TextAlignment.CENTER);


                //图片数据
                Image image = new Image("file:" + files[i].getAbsolutePath(), 100, 100, true, true, true);
                ImageView imageView = new ImageView(image);
                imageView.setFitHeight(100);
                imageView.setFitWidth(100);
                imageView.setPreserveRatio(true);

                //添加到interBox并设置中部对齐
                interBox.getChildren().add(imageView);
                interBox.setAlignment(Pos.BOTTOM_CENTER);

                //将图片和文本添加到总VBox中排列
                vBox.getChildren().addAll(picAnchor, text);
                list.add(vBox);
            }
        }
//        beanFactory.setBean("stringList", stringList);
        return list;
    }

    private void imgBoxOnClick(MouseEvent mouseEvent) {
        myMenuContext.hide();
        //改变图片样式
        if (mouseEvent.getButton() == MouseButton.PRIMARY && !rb.openDrag)
            changeImageStyle(mouseEvent);

//
        //开启鼠标右键选择
        if (mouseEvent.getButton() == MouseButton.SECONDARY)
            myMenuContext.show(stage, stage.getX() + 7.5 + mouseEvent.getSceneX(), stage.getY() + 30.0 + mouseEvent.getSceneY());

//        //改变左下角信息
        changeImageInfo();
//        //设置快捷键（以后调到加载后）

    }

    private void initMnemonic() {
        Scene mainScene = stage.getScene();
        KeyCombination key = KeyCombination.valueOf("ctrl+alt+c");
        KeyCombination key2 = KeyCombination.valueOf("ctrl+alt+v");
//            KeyCombination key3 = KeyCombination.valueOf("ctrl+alt+a");
        Mnemonic mc = new Mnemonic(copyButton, key);
        Mnemonic mc2 = new Mnemonic(pasteButton, key2);
//            Mnemonic mc3 = new Mnemonic(null,key3);
        mainScene.addMnemonic(mc);
        mainScene.addMnemonic(mc2);
//            mainScene.initMnemonic(mc3);
    }


    private void changeImageStyle(MouseEvent mouseEvent) {
        if (!mouseEvent.isControlDown()) {
            for (Node child : imgViewBox.lookupAll("ImageView")) {
                cancelSelectStyle(child.getParent().getParent().getParent());
            }
        }
        if (mouseEvent.getTarget() instanceof AnchorPane) {
            AnchorPane imageView = (AnchorPane) mouseEvent.getTarget();
            //点击事件
            ImageView iv = (ImageView) imageView.lookup("ImageView");
            VBox vBox = (VBox) iv.getParent().getParent().getParent();
            selectStyle(vBox);
            openPicture(mouseEvent, iv.getImage().getUrl());

        } else if (mouseEvent.getTarget() instanceof ImageView) {//点击图片位置
            ImageView iv = (ImageView) mouseEvent.getTarget();
            VBox vBox = (VBox) iv.getParent().getParent().getParent();
            selectStyle(vBox);
            //点击事件
//            beanFactory.setBean("clickImage", iv.getImage().getUrl());
            openPicture(mouseEvent, iv.getImage().getUrl());
        } else if (mouseEvent.getTarget() instanceof VBox) {
            VBox vBox = (VBox) mouseEvent.getTarget();
            if (vBox.getChildren().size() == 1) {

                selectStyle(vBox.getParent().getParent());
                openPicture(mouseEvent, ((ImageView) vBox.getParent().getParent().lookup("ImageView")).getImage().getUrl());
                return;
            }
            //点击事件
            ImageView iv = (ImageView) vBox.lookup("ImageView");
//            beanFactory.setBean("clickImage", iv.getImage().getUrl());

            selectStyle(vBox);
            openPicture(mouseEvent, iv.getImage().getUrl());

        } else if (mouseEvent.getTarget() instanceof Text) {
            Text text = (Text) mouseEvent.getTarget();
            VBox vBox = (VBox) text.getParent();
            selectStyle(vBox);
            //点击事件
            ImageView iv = (ImageView) vBox.lookup("ImageView");
//            beanFactory.setBean("clickImage", image.getImage().getUrl());
            openPicture(mouseEvent, iv.getImage().getUrl());

        }
    }

    private void openPicture(MouseEvent event, String clickImage) {
        if (event.getClickCount() != 2) {
            return;
        }
        LoadPlay lp = new LoadPlay();
        Set<Node> imageView = imgViewBox.lookupAll("ImageView");
        List<String> imageFileNameList = new ArrayList<>();
        for (Node node : imageView) {
            imageFileNameList.add(((ImageView) node).getImage().getUrl());
        }
        lp.loadPlay();
        lp.setImageFileNameList(imageFileNameList);//设置幻灯片播放列表
        lp.initImagePre(clickImage);//显示图片的前置处理
        lp.initBigSmallPref();//
        lp.initFunction();
        lp.show();//显示
    }

    private void changeImageInfo() {
        long size = 0;
        long imageNum = 0;
        long selectNum = 0;
        long selectSize = 0;
        Set<Node> nodes = imgViewBox.lookupAll("ImageView");
        for (Node node : nodes) {
            if (node.getParent().getParent().getParent().getOpacity() != 1) {
                selectNum++;
            }
        }
        Set<Node> imageView = imgViewBox.lookupAll("ImageView");
        if (imageView.size() == 0) {
            selectInfo.setText("0个项目");
            return;
        }
        for (Node node : imageView) {
            if (node instanceof ImageView) {
                ImageView iv = (ImageView) node;
                String filename = iv.getImage().getUrl().replace("file:", "");
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(filename);
                    int available = fileInputStream.available();
                    if (iv.getParent().getParent().getParent().getOpacity() != 1) {
                        selectSize += available;
                    }
                    size += available;
                    imageNum++;
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (fileInputStream != null)
                            fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        double mb = 1024 * 1024;
        String formatSize = String.format("%.2f", size * 1.0 / mb);
        String forMatSelectSize = String.format("%.2f", selectSize * 1.0 / mb);
//        size--->Mb
        selectInfo.setText(imageNum + "张图片(" + formatSize + "MB)" + "-选中" + selectNum + "张图片" + "(" + forMatSelectSize + "MB)");
    }

    private void selectedRectangle(RectBox rb) {
        imageViewOuter.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
            rb.hBoxView = new HBox();
            //删除
            rb.hBoxView.setId("Rect");
            //起始坐标
            rb.startX = e.getX();
            rb.startY = e.getY();
            rb.computeXStart = e.getSceneX();
            rb.computeYStart = e.getSceneY();
            if (e.isDragDetect()) {
                rb.openDrag = true;
            } else {
                rb.openDrag = false;
            }
        });
        imageViewOuter.setOnDragDetected(e -> {

            imageViewOuter.getChildren().add(rb.hBoxView);
            //矩形框样式
            rb.hBoxView.setBackground(new Background(new BackgroundFill(Paint.valueOf("#aaccee8f"), null, null)));
            rb.hBoxView.setBorder(new Border(new BorderStroke(Paint.valueOf("#0078d7"), BorderStrokeStyle.SOLID, null, new BorderWidths(1))));
            //起点偏移
            AnchorPane.setLeftAnchor(rb.hBoxView, rb.startX);
            AnchorPane.setTopAnchor(rb.hBoxView, rb.startY);

            //开启拖动
            rb.openDrag = true;

            imageViewOuter.startFullDrag();
        });
        imageViewOuter.setOnMouseDragOver(e -> {
            double sceneX = e.getX();
            double sceneY = e.getY();
            double width;
            double height;
            /*
             xMin = Math.min(rb.computeXStart, rb.computeXEnd);
             xMax = Math.max(rb.computeXStart, rb.computeXEnd);
             yMin = Math.min(rb.computeYStart, rb.computeYEnd);
             yMax = Math.max(rb.computeYStart, rb.computeYEnd);
            */
            if (sceneX < rb.startX) {
                //起点偏移x往右走
                AnchorPane.setLeftAnchor(rb.hBoxView, sceneX);
                width = rb.startX - sceneX;
            } else {
                AnchorPane.setLeftAnchor(rb.hBoxView, rb.startX);
                width = sceneX - rb.startX;
            }
            if (sceneY < rb.startY) {
                AnchorPane.setTopAnchor(rb.hBoxView, sceneY);
                height = rb.startY - sceneY;
            } else {
                //起点偏移
                AnchorPane.setTopAnchor(rb.hBoxView, rb.startY);
                height = sceneY - rb.startY;
            }
            double computeXEnd = e.getSceneX();
            double computeYEnd = e.getSceneY();
            rectSelectImage(rb.computeXStart, computeXEnd, rb.computeYStart, computeYEnd);
            rb.hBoxView.setPrefWidth(width);
            rb.hBoxView.setPrefHeight(height);
            changeImageInfo();
        });
        imageViewOuter.setOnMouseReleased(e -> {
            if (!rb.openDrag) {
                return;
            }
            //结束位置
            if (e.getX() < 0) {
                rb.computeXEnd = treeView.getWidth();
                rb.endX = 0;
            } else {
                rb.endX = e.getX();
                rb.computeXEnd = e.getSceneX();
            }
            if (e.getY() < 0) {
                rb.computeYEnd = 30.0;
                rb.endY = 0;
            } else {
                rb.endY = e.getY();
                rb.computeYEnd = e.getSceneY();
            }
            Node rect = imageViewOuter.lookup("#Rect");
            imageViewOuter.getChildren().remove(rect);

            //改变矩形框内图片
//            rectSelectImage();
            //若右键则显示菜单栏
            if (e.getButton() == MouseButton.SECONDARY)
                myMenuContext.show(imageViewOuter, stage.getX() + e.getSceneX(), stage.getY() + e.getSceneY());
        });
    }

    /**
     * @Description ojbk
     * @author lemon
     */
    //检测矩形框内是否有图片
    private void rectSelectImage(double xmin, double xmax, double ymin, double ymax) {
        if (!rb.openDrag) {
            return;
        }

        double xMin = Math.min(xmin, xmax);
        double xMax = Math.max(xmin, xmax);
        double yMin = Math.min(ymin, ymax);
        double yMax = Math.max(ymin, ymax);


        Set<Node> imageView = imgViewBox.lookupAll("ImageView");


        for (Node item : imageView) {
            ImageView iv = (ImageView) item;
            VBox vBox = (VBox) iv.getParent().getParent().getParent();
            double fitWidth = vBox.getWidth();
            double fitHeight = vBox.getHeight();
            Transform transform = vBox.getLocalToSceneTransform();
            double centerX = transform.getTx() + fitWidth / 2;
            double centerY = transform.getTy() + fitHeight / 2;
            if (centerX - xMin > 5 && xMax - centerX > 5 && centerY - yMin > 5 && yMax - centerY > 5) {
                selectStyle(vBox);
            } else {
                cancelSelectStyle(vBox);
            }
        }

    }


    //todo: 点击更改样式
    private void selectStyle(Node node) {
        node.setOpacity(0.95);
        node.setStyle("-fx-background-color: #cbe7f8;");
    }

    //todo: 点击取消样式
    private void cancelSelectStyle(Node node) {
        node.setStyle("");
        node.setOpacity(1);
    }

    //todo: 移动更改样式
    private void enterStyle(Node pane) {
        if (pane.getOpacity() == 1) {
            pane.setStyle("-fx-background-color: #e5f3fb;");
            pane.setOpacity(0.99);
        }
    }

    //todo: 移动取消样式
    private void cancelEnterStyle(Node pane) {
        if (pane.getOpacity() == 0.99) {
            pane.setStyle("");
            pane.setOpacity(1);
        }
    }

    public void addStyle(String cssPath) {
        mainViewWindow.getStylesheets().add(cssPath);
    }
}
